<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class App extends CI_Controller {

	// 
	//  index.php
	//  www.6block.com
	//  
	//  Created by Will_Lee on 2011-05-17.
	//  Copyright 2011 lishuzu. All rights reserved.
	// 
	function __construct()
	{
		parent::__construct();
		$this->config->load('app');
		$this->app_config = $this->config->item('app');
		$this->load->model('guest_model');
	}
	
	public function sina($appid,$param = '')
	{
		
		if (empty($appid)) {
			showmessage('请求错误', '', 3);
		}
		//sina微博接入
		$this->load->helper('sinaweibo/sina_weibooauth');
		$appid = intval($appid);
		$app_user = $this->session->userdata($appid.'_app_user') ? $this->session->userdata($appid.'_app_user') : '';
		$config = $this->app_config[$appid];
		if (empty($app_user)) {
			//sina微博登陆验证
			//sina微博
			$oauth = new WeiboOAuth($config['akey'], $config['skey']);
			$app_keys = $oauth->getRequestToken();
			$callback = base_url().'app/sina_login/'.$appid.'/'.$param;
			$authurl = $oauth->getAuthorizeURL( $app_keys['oauth_token'] ,TRUE , $callback );
			$this->session->set_userdata(array($appid.'_app_keys' => $app_keys));
			if ($authurl) {
				redirect($authurl);	
			}else{
				showmessage('对不起出现了点小问题，请稍后再试', '', 3);
			}

		}else{
			redirect($config['appurl']);
		}
	}
	
	function sina_login($appid,$param =''){
		$appuser = array();
		if (empty($appid)) {
			showmessage('请求错误', '', 3);
		}
		//sina微博接入
		$this->load->helper('sinaweibo/sina_weibooauth');
		
		$appid = intval($appid);
		$app_keys = $this->session->userdata($appid.'_app_keys');
		//sina微博登陆验证
		//sina微博
		$config = $this->app_config[$appid];
		$login = new WeiboOAuth( $config['akey'], $config['skey'] , $app_keys['oauth_token'] , $app_keys['oauth_token_secret']);
		$last_key = $login->getAccessToken($_REQUEST['oauth_verifier']);
		$app_keys['last_key'] = $last_key;
		$sina_client = new WeiboClient( $config['akey'], $config['skey'], $app_keys['last_key']['oauth_token'] , $app_keys['last_key']['oauth_token_secret']  );
		$appuser = $sina_client->verify_credentials();
		$userinfo = array(
				'name' => $appuser['name'],
				'avatar' => $appuser['profile_image_url'],
				'from' => 'sina',
				'sina_uid' => $appuser['id'],
				'weibo_url' => SINA_WB_URL.$appuser['id'],
				'dateline' => now(),
				'lastlogin' => now(),
			);
		// 访客
		if ($guest = $this->guest_model->get_guest_where(array('sina_uid' => $userinfo['sina_uid']))) {
			$updateguest = array(
				'guestid' => $guest['guestid'],
				'lastlogin' => now(),
				'name' => $appuser['name'],
				);
			$this->guest_model->update_guest($updateguest);
		}else{
			$this->guest_model->insert_guest($userinfo);
		}

		//加载登录信息
		$session_data = array(
			$appid.'_app_keys' => $app_keys,
			$appid.'_app_user' => $userinfo,
			);
		$this->session->set_userdata($session_data);
		redirect($config['appurl'].$param);
		unset($login);
		unset($sina_client);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/index.php */